/**
 * 购买订单
 */

define(function (require, exports, module) {


    var UI = require("js/UI")


    var $listFilterForm = $("#listFilterForm");
    var $dealType = $("#dealType"), $orderSelect = $("#orderSelect");


    var $gameInfoSelect = $("#gameInfoSelect");
    var $gameSelect = $gameInfoSelect.find(".G_select");

    UI.dropDownSelect($dealType);
    UI.dropDownSelect($orderSelect)
    var gameInst = null, areaInst = null, serverInst = null;

    var $gameSelectWrap = $gameSelect.eq(0);
    var $gName = $gameSelectWrap.find("input[name='gName']");

    var $areaSelectWrap = $gameSelect.eq(1);
    var $aName = $areaSelectWrap.find("input[name='aName']");

    var $serverSelectWrap = $gameSelect.eq(1);
    var $sName = $serverSelectWrap.find("input[name='sName']");


    var getGame = "/user/game/getGame";
    getGame = "/json/gameData.json"
    UI.ajax(getGame, "get", {}, function (flag, data) {
        var games = data.game;
        var gameData = $.map(games, function (g) {
            return {value: g.id, text: g.name}
        })
        gameInst = new UI.dropDownSelect($gameSelect.eq(0));
        gameInst.updateOption(gameData);
        gameInst.selected(function (gid, gName) {
            areaInst && areaInst.reset();
            serverInst && serverInst.reset();
            $gName.val(gName);
            var getDetailInfo = "/user/game/getDetailInfo";
            getDetailInfo = getGame;
            UI.ajax(getDetailInfo, "get", {gid: gid}, function (flag1, SAData) {
                //填充大区选项
                var areaData = SAData.area.area, serverData = SAData.area.server;
                var AData = $.map(areaData, function (a) {
                    return {value: a.aid, text: a.name}
                })
                areaInst = new UI.dropDownSelect($gameSelect.eq(1));
                areaInst.updateOption(AData);
                //选择服务器
                areaInst.selected(function (aid, aName) {
                    $aName.val(aName);
                    serverInst && serverInst.reset();
                    var SData = $.map(serverData, function (a) {
                        if (a.aid * 1 == aid) {
                            return {value: a.aid, text: a.name}
                        }
                    })
                    serverInst = new UI.dropDownSelect($gameSelect.eq(2));
                    serverInst.updateOption(SData);
                    serverInst.selected(function (sid, sName) {
                        $sName.val(sName);
                    })
                })
            })
        })
    })


    //<editor-folder desc='时间选择器'>
    var $dataTime = $("#orderTime input.G_ipt"), $dataTimePrev = $dataTime.eq(0),
        $dataTimeNext = $dataTime.eq(1);
    var resetPrev = function (val) {
        $dataTimePrev.datepicker("destroy");
        UI.datetimepicker($dataTimePrev, {
            maxDate: val,
            onSelect: function (data, inst) {
                resetNext(data)
            }
        })
    }
    var resetNext = function (val) {
        $dataTimeNext.datepicker("destroy");
        UI.datetimepicker($dataTimeNext, {
            minDate: val,
            onSelect: function (data, inst) {
                resetPrev(data)
            }
        })
    }
    UI.datetimepicker($dataTimePrev, {
        beforeShow: function (input, data) {

            setTimeout(function () {
                $("#ui-datepicker-div").css("zIndex", 9);
            }, 500)

        },
        onSelect: function (data, inst) {
            resetNext(data)
        }
    });
    UI.datetimepicker($dataTimeNext, {
        onSelect: function (data, inst) {
            resetPrev(data)
        }
    });
    //</editor-folder>


    var $filterForm = $listFilterForm.find("form");


    $filterForm.find("[type='reset']").unbind("click.reset").bind("click.reset", function () {
        var $select = $filterForm.find(".G_select");
        $select.each(function () {
                var $this = $(this), $tit = $this.find(".G_ipt"),
                    defaultValue = $tit.attr("data-default");
                $this.find("input").val("");
                if (defaultValue) {
                    $tit.text(defaultValue)
                } else {
                    $tit.text("")
                }
            }
        )
    })


})